package org.apache.flink.connector.jdbc.databases.mysql.dialect;

import org.apache.flink.connector.jdbc.statement.FieldNamedPreparedStatement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import static org.apache.flink.connector.jdbc.statement.FieldNamedPreparedStatementImpl.prepareStatement;

/**
 * @program: flink-connector-jdbc-3.1.1
 * @description: ${description}
 * @author: Copyright(c)2022-2032 carlos All Rights Reserved
 * @create by xiaolong.guo on 2023/10/17 16:20
 */
public class testFieldNamedPreparedStatement {

    public static void main(String[] args) {
        
        String[] fieldNames = {"id", "name", "email", "ts", "field1", "field_2", "__field_3__"};
        
        String sql = "INSERT INTO `tb1`(id, name, email, ts, field1, field_2, __field_3__) \n" +
                "VALUES (:id, :name, :email, :ts, :field1, :field_2, :__field_3__) ON DUPLICATE KEY \n" +
                "UPDATE `id`=VALUES(`id`), \n" +
                "`name`=VALUES(`name`), \n" +
                "`email`=VALUES(`email`), \n" +
                "`ts`=VALUES(`ts`), \n" +
                "`field1`=VALUES(`field1`), \n" +
                "`field_2`=VALUES(`field_2`), \n" +
                "`__field_3__`=VALUES(`__field_3__`)";

        Connection connection = getConnectMysql();

        try {
            FieldNamedPreparedStatement prepared = prepareStatement(connection, sql, fieldNames);
            System.out.print("prepareStatement方法返回结果："  + prepared);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
    }

    public static Connection getConnectMysql() {
        Connection connection = null;
        try {
            // 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 数据库连接地址
            String url = "jdbc:mysql://hadoop105:3306/testdb?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai";

            // 数据库用户名
            String username = "root";

            // 数据库密码
            String password = "root";

            // 创建Connection连接
            connection = DriverManager.getConnection(url, username, password);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
}
